<?php

namespace App\Http\Controllers\Manage;

use App\Helpers\ResponseData;
use App\Helpers\ServiceTrait;
use App\Http\Controllers\Controller;
use App\Models\Business\PlatformType;
use App\Models\CustomerManager;
use App\Models\CustomerManagerShop;
use App\Models\Department;
use App\Models\Shop;
use App\Models\ShopDetail;
use App\Models\SubAccount\SubAccount;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class CustomerPrepareController extends Controller
{
    use ResponseData,ServiceTrait;
    public function list(Shop $model,Request $request){
//        $user = $request->user();
//        if($user->data_permission){
//            $model = $this->model($model,$user);
//        }
        if(!$request->filled('customer_id')){
            return $this->failed('顾客id不存在',201);
        }
        $size = $request->input('size',10);
        $model = $model->where('customer_id',$request->customer_id);
        $data = $model->orderBy('id','desc')->paginate($size);
        return $this->success($data);
    }
    protected function getPlateformName($id){
        return PlatformType::query()->where('id',$id)->value('platform_name');
    }
    public function store(Request $request){
        $manager = CustomerManager::query()->find($request->customer_id);
        if(!$manager){
            return $this->failed('顾客用户不存在',201);
        }
        if(!$request->filled('plateform_id')){
            return $this->failed('平台id不存在',201);
        }
        $plateform_id = $request->plateform_id;
        $arr = explode(',',$plateform_id);
        if(count($arr)>0){
            foreach ($arr as $k=>$v){
                $data[$k]['customer_id'] = $request->customer_id;
                $data[$k]['plateform_name'] = $this->getPlateformName($v);
                $data[$k]['plateform_id'] = $v;
                $data[$k]['group_id'] = Str::uuid();
                $data[$k]['counsulting_volume'] = $request->counsulting_volume;
                $data[$k]['base_price'] = $request->base_price;
                $data[$k]['object_name'] = $request->object_name;
                $data[$k]['settlement_group'] = $request->settlement_group;
            }
            DB::table('shops')->insert($data);
        }

        return $this->success('保存成功');
    }

    public function edit(Shop $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }

    public function update(Shop $model,Request $request){
        $data = $model->find($request->id);
        $data->update($request->all());
        return $this->success('修改成功');
    }
    public function destory(Shop $model,Request $request){
        $data = $model->find($request->id);
        $data->delete();
    }
}
